import sys
sys.path.append("../")

from services.BaseService import *
from services.DataService import *
from model.CommonDb import *

class MenuService(BaseService):
    def __init__(self):
        super(MenuService,self).__init__()
        self.dataService = DataService()

    def getMenuObj(self):
        rs = self.dataService.getData("/query/sys_permission/select|*/where|parent_id=@0@",{})
        return self.recursionFn(rs[0], "sys_permission")

    def getFontMenuObj(self):
        rs = self.dataService.getData("/query/index_menu/select|*/where|parent_id=@0@",{})
        return self.recursionFn(rs[0], "index_menu")

    def recursionFn(self, t, tablename):
        childList = self.getChildList(t, tablename)
        t["children"] = childList
        for tChild in childList:
            if self.hasChild(tChild, tablename):
                self.recursionFn(tChild,tablename)
        return t

    def getChildList(self, t, tablename):
        rs = self.dataService.getData("/query/" + tablename + "/select|*/where|parent_id=@" + str(t["id"]) + "@",{})
        if len(rs) > 0:
            return rs
        else:
            return []

    def hasChild(self, t, tablename):
        rs = self.getChildList(t, tablename)
        if len(rs) > 0 :
            return True
        else:
            return False

    def getMenuList(self):
        return self.dataService.getData("/get_tree/sys_permission/9999/1",{})

    def getRols(self,roleid):
        return self.dataService.getData("/query/role/select|*/where|id="+roleid,{})
